require('../../css/jeui/skin/jebox.css');
require('./index.less');

import $ from 'jquery';

$(".serch-card").click(function() {
        if ($(".name").val() == "" && $(".cardnum").val() == "" && $(".phone").val() == "" && $(".idnum").val() == "") {
            jeBox.msg("请输入查询的内容", { icon: 3, time: 3 })
        } else {
            var cardinfo = { "cardOwner": $(".name").val(), "cardFaceId": $(".cardnum").val(), "status": 0, "contact": $(".phone").val(), "idNumber": $(".idnum").val() };
            var card_data_json = { "id": "1", "jsonrpc": "2.0", "method": "getYearlyCard", "params": { "record": cardinfo } };
            var carddata = $.ajax({
                type: "post",
                contentType: "application/json;charset=utf-8",
                url: "/api/baseInfoApi",
                dataType: "json",
                data: JSON.stringify(card_data_json),
            })
            carddata.then(function(data) {
                if (data.result.code == 200) {
                    var card_info_html = $("#card-info").html();
                    if (data.result.body.length == 0) {
                        jeBox.msg("查询失败,请重新输入查询内容", { icon: 3, time: 3 })
                    } else {
                        var carddtd = data.result.body[0];
                        carddtd.endDate = formattime(data.result.body[0].endDate);
                        carddtd.status = getstatus(data.result.body[0].status)
                        var conReg = /(?:\{)(\w*)(?:\})/g;
                        var source = card_info_html.replace(conReg, function(node, key) {
                            return carddtd[key];
                        });
                        $(".card-info").empty().append(source)
                    }

                } else if (data.result == undefined) {
                    jeBox.msg("您输入的查询内容无效", { icon: 3, time: 3 })
                }
            })
        }

    })
    //格式化时间 2016-07-09 17:01:34
function add0(m) {
    return m < 10 ? '0' + m : m
}

function formattime(t) { //如果要输出时间，time=true
    var time = new Date(t);
    var y = time.getFullYear();
    var m = time.getMonth() + 1;
    var d = time.getDate();
    var h = time.getHours();
    var mm = time.getMinutes();
    var s = time.getSeconds();
    return y + '-' + add0(m) + '-' + add0(d);
};

function getstatus(s) {
    if (s) {
        return "有效";
    } else {
        return "已过期";
    }
}